import Vue from "vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App.vue";
import router from "./router";
Vue.config.productionTip = false;
// 边框所需
import dataV from "@jiaminghi/data-view";

Vue.use(dataV);
import PubSub from "pubsub-js";
import httpRequest from "./utils/httpRequest";
import VueCookie from "vue-cookie";
Vue.use(VueCookie);
Vue.prototype.$http = httpRequest; // ajax请求方法
Vue.prototype.PubSub = PubSub; //消息订阅与发布
// 图标
import "@/assets/font_yi5g4ppdc1o/iconfont.css";
import "@/assets/ai/iconfont.css";
import "@/assets/font_wkyzptkhm6j/iconfont.css";
// 特效
import VueParticles from "vue-particles";
Vue.use(VueParticles);
// 无缝滚动
import scroll from "vue-seamless-scroll";
Vue.use(scroll);

Vue.use(ElementUI);
const vm = new Vue({
  router,
  render: (h) => h(App),
  beforeCreate() {
    Vue.prototype.$bus = this; //安装全局事件总线
  },
}).$mount("#app");

// 这个钩子函数 每一次路由跳转都会经过这个钩子函数  登录拦截页面
router.beforeEach((to, from, next) => {
  const WriteName = [
    "school",
    "department/level",
    "department/people",
    "person",
  ];
  // let cookie = document.cookie;
  // console.log(cookie);
  // console.log(document.cookie.split("username=").length);
  // console.log(to.name);

  // if (to.path === "/logout") {
  //   vm.$message.success("成功退出系统!");
  //   // 清除session
  //   window.sessionStorage.clear();
  //   // 跳转登录页
  //   next("/login");
  // } else 
  
  if (to.path === "/login") {
    vm.$message.success("成功退出系统!");
    window.sessionStorage.clear();
    next();
  } else if (WriteName.indexOf(to.name) >= 0) {
    // 如果是需要登录后访问的页面，检查用户是否已登录
    if (document.cookie.split("username=").length > 1) {
      next(); // 已登录，继续路由导航
    } else {
      // 未登录，弹出提示框并跳转到登录页
      vm.$message.error("请先登录系统!");
      next("/login");
    }
  } else if (document.cookie.split("username=").length <= 1) {
    vm.$message.error("请先登录系统!");
    next("/login");
  }
  next();
});
